package com.assic.utils.vo;

import java.util.HashMap;
import java.util.Map;

/**
 * 
 * @author Kerbores Result.java Create at 2014年1月4日
 * 
 */
public class Result {
	/**
	 * 创建一个带失败信息的result
	 * 
	 * @param reason
	 *            失败原因
	 * @return
	 */
	public static Result fail(String reason) {
		Result result = new Result();
		Map data = new HashMap();
		data.put("reason", reason);
		result.setData(data);
		result.setOperationState(OperationState.FAIL);
		return result;
	}

	/**
	 * 创建一个成功结果
	 * 
	 * @return
	 */
	public static Result success() {
		Result result = new Result();
		result.setOperationState(OperationState.SUCCESS);
		return result;
	}

	/**
	 * 获取一个result实例
	 * 
	 * @return
	 */
	public static Result me() {
		return new Result();
	}

	/**
	 * 操作结果数据 假设一个操作要返回很多的数据 一个用户名 一个产品 一个相关产品列表 一个产品的评论信息列表 我们以key
	 * value形式进行保存，页面获取data对象读取其对于的value即可
	 */
	private Map<String, Object> data;

	/**
	 * 带状态的操作 比如登录有成功和失败
	 */
	private OperationState operationState = OperationState.DEFAULT;
	/**
	 * 用于在jsp中显示标题的字段 title
	 */
	private String title;

	public Result() {
		super();
	}

	public Result(OperationState operationState, Map<String, Object> data, String title) {
		super();
		this.operationState = operationState;
		this.data = data;
		this.title = title;
	}

	/**
	 * 清空结果
	 */
	public void clear() {
		this.operationState = OperationState.DEFAULT;
		this.data.clear();
		this.title = "";
	}

	public Map<String, Object> getData() {
		return data;
	}

	public OperationState getOperationState() {
		return operationState;
	}

	public String getTitle() {
		return title;
	}

	public Result setData(Map<String, Object> data) {
		this.data = data;
		return this;
	}

	public Result setOperationState(OperationState operationState) {
		this.operationState = operationState;
		return this;
	}

	public Result setTitle(String title) {
		this.title = title;
		return this;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "Result [operationState=" + operationState + ", data=" + data + ", title=" + title + "]";
	}
}
